package action;

import bean.Person;
import com.opensymphony.xwork2.Action;

import dao.PersonDAO;
import dao.PersonDAOImpl;
import factory.HibernateSessionFactory;

import java.util.List;
import java.util.Objects;

public class PersonAction implements Action {
    // 此项目为演示项目，没有设计service
    private PersonDAO personDAO = new PersonDAOImpl(HibernateSessionFactory.getSession());
    private List<Person> persons;
    private Person person;

    public List<Person> getPersons() {
        return persons;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    @Override
    public String execute() throws Exception {
        persons = personDAO.queryAll();
        return SUCCESS;
    }

    public String add() {
        return personDAO.add(person) ? "add" : null;
    }

    public String edit() {
        person = personDAO.query(person.getPersonId());
        return Objects.nonNull(person) ? "edit" : null;
    }

    public String modify() {
        return personDAO.modify(person) ? "modify" : null;
    }

    public String remove() {
        return personDAO.remove(person.getPersonId()) ? "remove" : null;
    }
}
